package com.songshuai.customer.client.service.impl;

import com.songshuai.customer.client.service.CustomerInfoService;
import com.songshuai.webapi.baseapi.constant.miniprogram.CacheKeys;
import com.songshuai.webapi.baseapi.model.sales.CustomerInfo;
import com.songshuai.webapi.common.redis.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseServiceImpl {

    @Autowired
    public RedisUtil redisUtil;
    @Autowired
    protected CustomerInfoService customerInfoService;

    //获取当前用户customerId   使用时记得判空
    public String getCurrentcustomerId (String token) {
        if (StringUtils.isNotBlank(token)) {
            Object o = redisUtil.hget(token, CacheKeys.PANGU_CUST_ID);
            if (null != o) {
                return (String) o;
            } else {
                CustomerInfo customerInfo = customerInfoService.selectByEntity(new CustomerInfo() {{
                    setToken(token);
                }});
                if (null != customerInfo) {
                    return customerInfo.getUserId();
                } else {
                    return null;
                }
            }
        } else {
            return null;
        }
    }
}
